package com.example.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.server.entity.User;
import com.example.server.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-11-21 09:10:56
 */
@RestController
@RequestMapping("user")
public class UserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<User> page, User user) {
        return success(this.userService.page(page, new QueryWrapper<>(user)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userService.getBaseMapper().selectOne(queryWrapper);
        if(user != null)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(user);
            return r;
        }
            r.setCode(404);
            r.setMsg("查询失败");

        return r;
    }

    //用户注册
    @PostMapping("/Register")
    public R Register(@RequestBody User user) {
        R r =new R<>();
        QueryWrapper queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("userName",user.getUsername());
        User re =userService.getBaseMapper().selectOne(queryWrapper1);
        QueryWrapper queryWrapper2 =new QueryWrapper<>();
        queryWrapper2.eq("email",user.getEmail());
        User rs =userService.getBaseMapper().selectOne(queryWrapper2);
        if(re==null&&rs==null)
        {
            userService.save(user);
            System.out.println("注册成功");
            Map rqLn=new HashMap<>();//登录要返回的数据
            rqLn.put("name",user.getUsername());
            r.setCode(200);
            r.setData(rqLn);
            r.setMsg("注册成功！");
            return r;
        }
        System.out.println("注册失败！");
        r.setMsg("注册失败！");
        r.setCode(400);
        return r;
    }
    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody User user) {
        return success(this.userService.updateById(user));
    }

    /**
     * 根据账户名和密码进行登录功能：根据username和password寻找
     */
    @PostMapping ("/LoginByName")
    public R Enter(@RequestBody User user) {
        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("userName",user.getUsername());
        User re = userService.getBaseMapper().selectOne(queryWrapper);
        if(re!=null){
            String pass =re.getPassword();
            if(pass.equals(user.getPassword())) {
                System.out.println("登录成功");
                Map rqLn=new HashMap<>();//登录要返回的数据
                rqLn.put("id",re.getId());
                rqLn.put("name",re.getUsername());
                rqLn.put("phone",re.getPhone());
                r.setCode(200);
                r.setData(rqLn);
                r.setMsg("登录成功");
                return r;
            }
        }
        System.out.println("登录失败");
        r.setMsg("登录失败");
        r.setCode(400);
        return r;
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.userService.removeByIds(idList));
    }
}

